package com.wfit.admin.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wfit.admin.common.Result;
import com.wfit.admin.entity.Role;
import com.wfit.admin.service.IRoleService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;


/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author 马真志
 * @since 2023-12-13
 */
@RestController
@RequestMapping("/role")
public class RoleController {

        @Resource
        private IRoleService roleService;

        @PostMapping
        public Boolean save(@RequestBody Role role) {
                return roleService.saveOrUpdate(role);
                }

        @DeleteMapping("/{id}")
        public Boolean delete(@PathVariable Integer id) {
                return roleService.removeById(id);
                }

        @GetMapping
        public List<Role> findAll() {
                return roleService.list();
                }

        @GetMapping("/{id}")
        public Role findOne(@PathVariable Integer id) {
                return roleService.getById(id);
                }

        @GetMapping("/page")
        public Page<Role> findPage(@RequestParam Integer pageNum,
                                   @RequestParam Integer pageSize,
                                   @RequestParam(defaultValue = "") String name) {

                QueryWrapper<Role> queryWrapper = new QueryWrapper<>();
                if (!"".equals(name)){
                        queryWrapper.like("name",name);
                }

                return roleService.page(new Page<>(pageNum, pageSize),queryWrapper);
        }
        @PostMapping("/roleMenu/{roleId}")
        public Result roleMenu(@PathVariable Integer roleId, @RequestBody List<Integer> menuIds) {
                roleService.setRoleMenu(roleId, menuIds);
                return Result.success();
        }


        @GetMapping("/roleMenu/{roleId}")
        public Result getRoleMenu(@PathVariable Integer roleId) {
                return Result.success( roleService.getRoleMenu(roleId));
        }
        }

